;;; sen-bindings.el --- Set up some handy key bindings
;;
;; Modified from the Emacs Starter Kit.
;; TODO: Make keybindgings  unified

;; You know, like Readline.
(global-set-key [(control meta ?h)] 'backward-kill-word)
;; Align your code in a pretty way.
(global-set-key [(control x) ?\\] 'align-regexp)
;; Completion that uses many different methods to find options.
(global-set-key [(meta-?/)] 'hippie-expand)
;; Perform general cleanup.
(global-set-key [(control c) ?n] (lambda () (interactive)
                                (indent-buffer)
                                (delete-trailing-whitespace)
                                (untabify-buffer)))
;; comments
(global-set-key [(control ?/)] 'comment-region)
(global-set-key [(control ??)] 'uncomment-region)
;; bookmarks 
(with-library bm 
              (global-set-key [f2] 'bm-toggle)
              (global-set-key [(control f2)]   'bm-next)
              (global-set-key [(meta f2)] 'bm-previous))

(global-set-key [(meta ?v)] 'yank-pop) ; instead of M-y
(global-set-key [(control ?v)] 'yank)     ; instead of C-y
(global-set-key [(meta up)] 'prh:move-line-up)
(global-set-key [(meta down)] 'prh:move-line-down)
(global-set-key [(control x) (control up)]  'prh:duplicate-line-up)
(global-set-key [(control x) (control down)] 'prh:duplicate-line-down)

;; highlight-symbol 
;; C-f3 to toggle highlighting of the symbol at point throughout the current buffer.  
;; Use highlight-symbol-mode to keep the symbol at point always highlighted.
;; The functions highlight-symbol-next, highlight-symbol-prev, 
;; highlight-symbol-next-in-defun and highlight-symbol-prev-in-defun allow for 
;; cycling through the locations of any symbol at point.
(with-library highlight-symbol
              (global-set-key [(control f3)] 'highlight-symbol-at-point) 
              (global-set-key [f3] 'highlight-symbol-next) 
              (global-set-key [(shift f3)] 'highlight-symbol-prev)
              (global-set-key [(meta f3)] 'highlight-symbol-prev))

; F6 stores a position in a file, Ctrl-F6 brings you back to this position
; rather than traditional C-x r SPC and C-x r j
;(global-set-key [f6] '(lambda () (interactive) (point-to-register ?1)))
;(global-set-key [(control f6)] '(lambda () (interactive) (register-to-point ?1)))


;; Turn on the menu bar for exploring new modes
(global-set-key [f1] 'menu-bar-mode)
(global-set-key [(control f1)] 'imenu-add-menubar-index)

;; Use regex searches by default.
(global-set-key [(control ?s)] 'isearch-forward-regexp)
(global-set-key [(control ?r)] 'isearch-backward-regexp)
(global-set-key [(control meta ?s)] 'isearch-forward)
(global-set-key [(control meta ?r)] 'isearch-backward)
;; Activate occur easily inside isearch
(define-key isearch-mode-map (kbd "C-o")
  (lambda () (interactive)
    (let ((case-fold-search isearch-case-fold-search))
      (occur (if isearch-regexp isearch-string (regexp-quote isearch-string))))))

;; Jump to a definition in the current file. (This is awesome.)
(global-set-key [(control x) (control i)] 'ido-goto-symbol)

;; File finding
(global-set-key [(f12)]      'recentf-open-most-recent-file)
(global-set-key [(meta f12)] 'recentf-open-files)
(global-set-key [(control x) (meta ?f)] 'ido-find-file-other-window)
(global-set-key [(control x) (control meta ?f)] 'find-file-in-project)
(global-set-key [(control x) ?f] 'recentf-ido-find-file)
(global-set-key [(control x) (control p)] 'find-file-at-point)
(global-set-key [(control c) ?y] 'bury-buffer)
(global-set-key [(control c) ?r] 'revert-buffer)
(global-set-key [(meta ?`)] 'file-cache-minibuffer-complete)
(global-set-key [(control x) (control b)] 'ibuffer)
;(global-set-key (kbd "C-,") 'bs-show)
;(global-set-key (kbd "C-.") 'ido-switch-buffer)
(global-set-key [(meta g) ?j] 'dired-jump)        ; jump to a directory of a current buffer
(global-set-key [(meta g) ?w] 'dired-jump-other-window) ; jump to a directory of a current buffer


;; Lisp
(define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol)
(define-key lisp-mode-shared-map (kbd "RET") 'reindent-then-newline-and-indent)
(define-key lisp-mode-shared-map (kbd "C-\\") 'lisp-complete-symbol)
(define-key lisp-mode-shared-map (kbd "C-c s") 'my-eval-and-replace)
(define-key lisp-mode-shared-map (kbd "C-c v") 'eval-buffer)


;; Fetch the contents at a URL, display it raw.
(global-set-key (kbd "C-x C-h") 'view-url)

;; Help should search more than just commands
(global-set-key (kbd "C-h a") 'apropos)

;; Applications
(global-set-key (kbd "C-c j") (lambda () (interactive) (switch-or-start 'jabber-connect "*-jabber-*")))
(global-set-key (kbd "C-c g") (lambda () (interactive) (switch-or-start 'gnus "*Group*")))
(global-set-key (kbd "C-c i") (lambda () (interactive) (switch-or-start (lambda ()
                                                                          (rcirc-connect "irc.freenode.net"))
                                                                   "*irc.freenode.net*")))
(global-set-key (kbd "C-c J") 'jabber-send-presence)
(global-set-key (kbd "C-c M-j") 'jabber-disconnect)



;; Flyspell additional stuff
;; TODO: break it into clean functions and bindings
(if (and (fboundp 'flyspell-goto-previous-error)
         (fboundp 'ispell-previous-error))
    (progn 
      (defun flyspell-mode-handler()
        (local-set-key (kbd "C-<") 'flyspell-goto-previous-error)
        (local-set-key (kbd "M-#") 'ispell-previous-error))

      (add-hook 'flyspell-mode-hook 'flyspell-mode-handler)
      (add-hook 'flyspell-prog-mode-hook 'flyspell-mode-handler)))

(provide 'init-bindings)
